﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebSite_t_data_relevance : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null || Session["UserIdentity"].ToString() != "1")
        {
            Response.Redirect("Default.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
    }
    protected void RelevanceBtn_Click(object sender, EventArgs e)
    {
        string grade1,grade2,collegename1, collegename2,targetname1, targetname2,sex1, sex2;

        //最小支持度和最小置信度
        double minup, mincon;

        //上限下限
        string up1, up2, low1, low2;

        //获得DropDownList中选中的信息
        grade1 = DropDownListcon_year1.SelectedItem.Text;
        grade2 = DropDownList2.SelectedItem.Text;
        collegename1 = DropDownList_con_major1.SelectedItem.Text;
        collegename2 = DropDownList3.SelectedItem.Text;
        targetname1 = DropDownList_item1.SelectedItem.Text;
        targetname2 = DropDownList4.SelectedItem.Text;
        sex1 = DropDownList1.SelectedItem.Text;
        sex2 = DropDownList5.SelectedItem.Text;

        //获得最小支持度和最小置信度
        minup = double.Parse(TextBox1.Text);
        mincon = double.Parse(TextBox2.Text);

        up1 = TextBox4.Text;
        low1 = TextBox3.Text;

        up2 = TextBox8.Text;
        low2 = TextBox7.Text;

        string sqlString1,sqlString2,sqlString3;
        sqlString1="select count(UserId) from PhysiqueTest ";
        bool IsBegin = false;

        if (grade1!="全部年级")
        {
            IsBegin = true;
            sqlString1 += "where UserId IN (select UserId from UserDetail where Grade=" + grade1;
        }
        if (collegename1!="全部院系")
        {
            if (!IsBegin)
            {
                sqlString1 += "where UserId In(select UserId from UserDetail where ";
                IsBegin = true;
            }
            else 
            {
                sqlString1+=" and ";
            }
            sqlString1 += "collegename='" + collegename1.Trim()+"'";
        }
        if (sex1!="全部")
        {
            if (!IsBegin)
            {
                sqlString1 += "where UserId In(select UserId from UserDetail where ";
                IsBegin = true;
            }
            else
            {
                sqlString1 += " and ";
            }
            if (sex1=="男")
            {
                sex1 = "0";
            }
            else { sex1 = "1"; }
            sqlString1 += "sex=" + sex1;
        }
        if (IsBegin)
        {
            sqlString1 += ")";
            sqlString2 = sqlString1+" and ";
        }
        else
        {
            sqlString2 = sqlString1;
            sqlString2 += " where ";
        }
        sqlString2 += "TestName='" + targetname1.Trim()+"'";
        sqlString2+=" and Result>="+low1+" and Result<="+up1;

        sqlString3 = sqlString2;
        IsBegin = false;
        if (grade2!="全部年级")
        {
            IsBegin = true;
            sqlString3 += " and UserID IN (select UserId from PhysiqueTest where UserId IN (select UserId from UserDetail where Grade=" + grade2;
        }
        if (collegename2!="全部院系")
        {
            if (!IsBegin)
            {
                sqlString3 += "  and UserID IN (select UserId from PhysiqueTest where UserId IN (select UserId from UserDetail where CollegeName='" + collegename2.Trim() + "'";
                IsBegin = true;
            }
            else
            {
                sqlString3 += " and CollegeName='" + collegename2.Trim() + "'";
            }
        }
        if (sex2!="全部")
        {
            if (!IsBegin)
            {
                sqlString3 += "  and UserID IN (select UserId from PhysiqueTest where UserId IN (select UserId from UserDetail where ";
                IsBegin = true;
            }
            else{
                sqlString3 += " and ";
            }
            if (sex2 == "男")
            {
                sex2 = "0";
            }
            else { sex2 = "1"; }

            sqlString3+="sex="+sex2;
        }
        if (IsBegin)
        {
            sqlString3+=") and ";
        }
        else 
        {
            sqlString3 += "and UserID IN (select UserId from PhysiqueTest where ";
        }
        sqlString3 += "TestName='" + targetname2 + "'";
        sqlString3 += " and Result<=" + up2 + " and Result>=" + low2+")";


        int totoalnum=GetNum(sqlString1);
        int firstnum = GetNum(sqlString2);
        int secondnum = GetNum(sqlString3);

        TotalNumLabel.Text = totoalnum.ToString();
        Label1.Text = firstnum.ToString();
        Label2.Text = secondnum.ToString();

        if (((float)(firstnum)/(float)(totoalnum))>=minup)
        {
            Label3.Text="满足最小支持度";
        }
        else
        {
            Label3.Text="不满足最小支持度";
        }

        if (((float)(secondnum)/(float)(totoalnum))>=mincon)
        {
            Label4.Text="满足最小置信度";
            Label5.Text = "满足您设定的条件，二者可能有联系，但是不排除有与之联系更加紧密的项。";
        }
        else 
        {
            Label4.Text="不满足最小置信度";
            Label5.Text="不满足您设定的条件，二者可能没有联系，但是不排除您最小支持度和置信度设置不合理的情况";
        }

    }
    private int GetNum(string sqlString)
    {
        try
        {
            SqlConnection conn = new SqlConnection("Data Source=Localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
            SqlCommand command = new SqlCommand(sqlString, conn);
            conn.Open();
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                return int.Parse(reader.GetValue(0).ToString());
            }
            conn.Close();
        }
        catch(Exception ex)
        {
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message;
            Session["AtPage"] = "t_data_relevance.aspx";
            Response.Redirect("t_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return 0;
        }
        return 0;
    }
}
